
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
	TreeNode* pruneTree(TreeNode* root) {
		int ret = dfs(root);
		if (ret == 0)root = nullptr;
		return root;
	}
	int dfs(TreeNode* root) {
		if (root->left == nullptr && root->right == nullptr)return root->val;
		int left = 0, right = 0;
		if (root->left)left = dfs(root->left);
		if (root->right)right = dfs(root->right);
		if (left == 0) root->left = nullptr;
		if (right == 0)root->right = nullptr;
		return root->val || left || right;
	}
};